package RJZ.Rainbow.Controller;

import RJZ.Rainbow.Config.Information;
import RJZ.Rainbow.Service.ChequerService;
import RJZ.Rainbow.Service.OrderService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

import static java.lang.Thread.sleep;

@Controller
public class SenserController implements Runnable {

    InputStream inputStream;
    BufferedReader in = null;
    PrintWriter out;

    public PrintWriter getOut() {
        return out;
    }
    @Autowired
    Information imfor;
    @Autowired
    ChequerService chequerService;
    @Autowired
    OrderService orderService;
    boolean link = false;
    Channel ch;

    public  void  link() {
        try {
            ServerSocket server = new ServerSocket(5000);
            System.out.println("等待传感器连接");
            Socket socket = server.accept(); //阻塞等待, 直到一个客户端 socket过来
            System.out.println("传感器连接成功！" + socket);
            inputStream=socket.getInputStream();
            in = new BufferedReader(new InputStreamReader(inputStream));
            out = new PrintWriter(socket.getOutputStream()); //输出，to 客户端
            link = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public void init(Channel ch) {
        this.ch = ch;
    }

    @Override
    public void run() {

        while (true) {
            try {
                if(inputStream.available()!=0)
                {
                    String msg=in.readLine();
                    System.out.println(msg);
                    JSONObject json = JSON.parseObject(msg);
                    String up = "{" +
                            "\"Cop\":" + "\"180\","+
                            "\"box_id\":\"" + imfor.getId()+"\","+
                            "\"boxlog\":" + msg+
                            "}" + '\n';
                    ch.writeAndFlush(up);
                    String cop = json.getString("Cop");
                    switch (cop) {
                        case "1": {//温度本地库
                            orderService.UpdateLocalTemperature(json.getInteger("door"), json.getInteger("temp"),ch);
                            continue;
                        }
                        case "141": {//打开箱子成功
                            boolean result = chequerService.OpenTheDoor(json.getInteger("opc"), json.getInteger("door"));
                            if (result) {
                                continue;
                            } else {
                                break;
                            }
                        }
                        case "142": {//投件关闭箱子成功
                            orderService.SubCloseTheDoor(json.getInteger("door"),ch);
                            break;
                        }
                        case "143": {//取件关闭箱子成功
                            orderService.TakeCloseTheDoor( json.getInteger("door"),ch);
                            break;
                        }
                    }
                    sleep(1000);
                }
            }
            catch (IOException e){
                System.out.println("传感器连接断开");
                link();

                e.printStackTrace();
                break;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
